/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package engine;

import dbo.Deployment;
import dbo.Player;
import dbo.Squad;
import java.util.Collections;
import java.util.Random;
import java.util.Vector;

/**
 *
 * @author bi
 */
public final class ArtificialIntelligent {

    public Player player;
    private Vector<Deployment> list;
    private Vector<Squad> shop;

    public ArtificialIntelligent(Player player, Vector<Deployment> list, Vector<Squad> shop) {
        this.player = player;
        this.list = new Vector<Deployment>();
        this.shop = new Vector<Squad>();
        loadList(list);
        loadShop(shop);
    }

    public void loadList(Vector<Deployment> list) {
        for (int i = 0; i < list.size(); i++) {
            Deployment d = list.get(i);
            if (d.player.equals(this.player)) {
                this.list.add(d);
            }
        }
    }

    public void loadShop(Vector<Squad> shop) {
        for (int i = 0; i < shop.size(); i++) {
            Squad s = shop.get(i);
            if (s.faction.equals(player.faction)) {
                this.shop.add(s);
            }
        }

        Collections.sort(shop);
    }

    public Squad makePurchaseDecision() {
        //Computer dont like to purchase when resource is low
        if (player.getRequisition()<=200) {
            return null;
        }

        // if the shop size is 0, return null
        if (shop.isEmpty()) {
            return null;
        } // if the shop size is 1, return that squad
        else if (shop.size() == 1) {
            return shop.get(0);
        }
        // otherwise, continue to go to the AI check
        float totalReq = player.getRequisition();
        float growReq = Constant.REQ_GROW;
        float maxReq = totalReq * 2 / 3;
        float minReq = totalReq / 3;
        int infAmount = 0;
        int vehAmount = 0;
        int backupAmount = 0;
        boolean hasHero = false;

        Random r = new Random(System.currentTimeMillis());
        int priorityBuilder = r.nextInt(35) + 5;
        int priorityInfantry = r.nextInt(30) + 10;
        int priorityVehicle = r.nextInt(40);
        int priorityHero = r.nextInt(10) + 30;

        Vector<Squad> shopPurchase = new Vector<Squad>();

        for (int i = 0; i < list.size(); i++) {
            Deployment d = list.get(i);
            if (d.squad.hasSkill("BuilderA")) {
                growReq += Constant.REQ_BUILDER_A;
            } else if (d.squad.hasSkill("BuilderB")) {
                growReq += Constant.REQ_BUILDER_B;
            } else if (d.squad.hasSkill("BuilderC")) {
                growReq += Constant.REQ_BUILDER_C;
            } else if (d.squad.hasSkill("Hero")) {
                hasHero = true;
            } else if (d.squad.hasSkill("Sharpshooter")
                    || d.squad.hasSkill("Artillery")
                    || d.squad.hasSkill("Stealth")) {
                backupAmount++;
            } else if (d.squad.hasSkill("Vehicle")
                    || d.squad.hasSkill("Walker")
                    || d.squad.hasSkill("Aircraft")) {
                vehAmount++;
            } else {
                infAmount++;
            }

        }

        //If min affordable requisition is higher than grow requisition rate,
        //a necessary builder must be built.
        if (minReq >= growReq) {
            priorityBuilder += 20;
        }
        //If max affordable requisition is higher than grow requisition rate,
        //choose to priority builder.
        if (maxReq >= growReq) {
            priorityBuilder += 10;
        }
        //If squad size is about to extinct, try infantry.
        if (list.size() < 3) {
            priorityInfantry += 25;
        }
        //If amount of infantry is too high compare to vehicle, priority vehicle
        if (infAmount + infAmount > vehAmount) {
            priorityVehicle += 25;
        } else {
            priorityInfantry += 15;
        }
        //Try to purchase more infantry as backup rate is high
        if (backupAmount > infAmount) {
            priorityInfantry += 10;
        }
        //Lower all other priority as the hero is not available
        if (!hasHero) {
            priorityHero += 10;
        }

        //Onto the purchase
        //Find the one who is highest
        String topItem = "Builder";
        int topRank = priorityBuilder;
        if (priorityInfantry > topRank) {
            topItem = "Infantry";
            topRank = priorityInfantry;
        }
        if (priorityVehicle > topRank) {
            topItem = "Vehicle";
            topRank = priorityVehicle;
        }
        if (priorityHero > topRank) {
            topItem = "Hero";
            topRank = priorityHero;
        }

        System.out.println(topItem);

        for (int i = 0; i < shop.size(); i++) {
            Squad s = shop.get(i);
            if (s.hasSkill("BuilderA")
                    || s.hasSkill("BuilderB")
                    || s.hasSkill("BuilderC")) {
                if (topItem.equals("Builder")) {
                    shopPurchase.add(s);
                }
            } else if (s.hasSkill("Vehicle")
                    || s.hasSkill("Walker")
                    || s.hasSkill("Aircraft")) {
                if (topItem.equals("Vehicle")) {
                    shopPurchase.add(s);
                }
            } else if (s.hasSkill("Hero")) {
                if (topItem.equals("Hero")) {
                    shopPurchase.add(s);
                }
            } else {
                if (topItem.equals("Infantry")) {
                    shopPurchase.add(s);
                }
            }
        }

        Squad purchaseSquad;
        if (shopPurchase.isEmpty()) {
            //Random Everything
            purchaseSquad = shop.get(r.nextInt(shop.size()));
        } else {
            purchaseSquad = shopPurchase.get(r.nextInt(shopPurchase.size()));
        }
        if (player.getRequisition() >= purchaseSquad.getCost()) {
            return purchaseSquad;
        } else {
            return null;
        }
    }
}
